package com.aigou.admin.dao.impl;

import com.aigou.admin.utils.JDBCUtils;
import com.aigou.admin.dao.SalesDao;
import com.aigou.admin.entity.Sales;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: May
 * @Date: 2024/12/3 16:32
 * @Version: 1.0
 */

@Repository
public class SalesDaoImpl implements SalesDao {
    @Override
    public List<Sales> selectAll() {
        return JDBCUtils.executeQuery(Sales.class, "select * from s_sales");
    }

    @Override
    public Sales selectById(String id) {
        List<Sales> sales = JDBCUtils.executeQuery(Sales.class, "select * from s_sales where id = ? ",  id);
        if (sales.isEmpty()) {
            return null;
        }
        return sales.get(0);
    }

    @Override
    public Boolean insert(Sales sales) {
        int rows = JDBCUtils.executeUpdate("insert into s_sales(id,product_id,sales_num) values(?,?,?)"
                , sales.getId(),sales.getProduct_id(),sales.getSales_num());
        return rows > 0;
    }

    @Override
    public Boolean update(Sales sales) {
        int rows = JDBCUtils.executeUpdate("update s_sales set product_id =?,sales_num=? where id = ?"
                , sales.getProduct_id(),sales.getSales_num(),sales.getId());
        return rows > 0;
    }

    @Override
    public Boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_sales where id = ?", id);
        return rows >0;
    }

    @Override
    public Integer selectUserCount() {
        return JDBCUtils.executeCount("select count(*) from s_sales");
    }

    @Override
    public List<Sales> selectUserLimit(int start, int rows) {
        return JDBCUtils.executeQuery(Sales.class,"select * from s_sales limit ?,?",start,rows);
    }

    @Override
    public List<Sales> findByNameContaining(String id) {
        return JDBCUtils.executeQuery(Sales.class,"select * from s_sales where id like '%"+id+"%'");

    }
}
